import { ApiCall } from "tsrpc";

import { sts } from "tencentcloud-sdk-nodejs";
import { ReqGetTempKey, ResGetTempKey } from "../shared/api_client/PtlGetTempKey";
const { Client } = sts.v20180813;

export default async function (call: ApiCall<ReqGetTempKey, ResGetTempKey>) {
    // TODO
    const client = new Client({
        credential: {
            secretId:"AKIDBOOAk6FJ9mALI2YqvlOnyvfBSVKwKTSE",
            secretKey:"by72kRzLUbU9anncDuQjofQxIQQFqDMH",
        },
        region: "ap-guangzhou",
    });

    const response = await client.GetFederationToken({
        Name: 'upload-token',
        Policy: JSON.stringify({
            version: '2.0',
            statement: [{
                action: ['cos:PutObject'],
                effect: 'allow',
                resource: 'qcs::cos:ap-guangzhou:uid/1251587647:dew-1251587647/*'
            }]
        }),
        DurationSeconds: 1800
    })
    console.log('STS Response:', JSON.stringify(response, null, 2));
    call.succ({
        TmpSecretId: response.Credentials?.TmpSecretId || '',
        TmpSecretKey: response.Credentials?.TmpSecretKey || '',
        SecurityToken: response.Credentials?.Token || '',
        StartTime: Math.floor(Date.now() / 1000),
        ExpiredTime: Math.floor(Date.now() / 1000) + 1800,
    })
}
